package yb.ecp.fast.user.service.auth.handler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import yb.ecp.fast.infra.infra.log.LogHelper;
import yb.ecp.fast.user.dao.entity.AccountDO;
import yb.ecp.fast.user.dao.entity.PasswordDO;
import yb.ecp.fast.user.dao.entity.PasswordDOKey;
import yb.ecp.fast.user.dao.entity.ProfileDO;
import yb.ecp.fast.user.dao.mapper.AccountMapper;
import yb.ecp.fast.user.dao.mapper.PasswordMapper;
import yb.ecp.fast.user.dao.mapper.ProfileMapper;
import yb.ecp.fast.user.exception.AuthException;
import yb.ecp.fast.user.infra.AuthConstant;
import yb.ecp.fast.user.infra.ErrorCode;
import yb.ecp.fast.user.manager.AuthInfoManager;
import yb.ecp.fast.user.service.AccountService;
import yb.ecp.fast.user.service.AsyncService;
import yb.ecp.fast.user.service.VO.UserLoginVO;

import javax.servlet.http.HttpServletRequest;

/**
 * 原来的登录体系
 * @author f00lish
 * @date 2021/7/8 3:54 下午
 */
@Order
@Component
public class OriginAccountHandler implements AccountHandler {

    @Autowired
    AccountMapper accountMapper;
    @Autowired
    PasswordMapper passwordMapper;
    @Autowired
    AuthInfoManager authInfoManager;
    @Autowired
    ProfileMapper profileMapper;

    @Autowired
    AsyncService asyncService;
    @Value("${login.recordIp.default.flag:N}")
    private String loginRecordIp;
    @Autowired
    private HttpServletRequest request;

    @Override
    public AccountDO handle(UserLoginVO userLoginVO, Integer site) {
        AccountDO accountDO = accountMapper.selectByPrimaryKey(userLoginVO.getLoginName());
        if (accountDO == null) {
            LogHelper.error("登录账号不存在！", ErrorCode.CheckLoginFailure.getCode());
            throw new AuthException(ErrorCode.CheckLoginFailure);
        }

        PasswordDOKey passwordDOKey = new PasswordDOKey();
        passwordDOKey.setUserId(accountDO.getUserId());
        passwordDOKey.setType(0);
        PasswordDO passwordDO = passwordMapper.selectByPrimaryKey(passwordDOKey);
        if (passwordDO == null) {
            LogHelper.error("登录密码不存在！", ErrorCode.CheckLoginFailure.getCode());
            throw new AuthException(ErrorCode.CheckLoginFailure);
        }

        ProfileDO profileDO = profileMapper.selectById(passwordDO.getUserId());
        if (profileDO == null || profileDO.getLocked() == AuthConstant.USER_LOCKED) {
            LogHelper.error("用户状态被锁！", ErrorCode.UserLocked.getCode());
            throw new AuthException(ErrorCode.UserLocked);
        }

        String cypherPassword = null;
        try {
            cypherPassword = AccountService.cryptoPassword(userLoginVO.getPassword(), accountDO.getUserId());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        if (!cypherPassword.equals(passwordDO.getPassword())) {
            LogHelper.error("登录密码错误！", ErrorCode.CheckLoginFailure.getCode());
            throw new AuthException(ErrorCode.CheckLoginFailure);
        }
        if("Y".equals(loginRecordIp)){
            asyncService.recordUserLoginRecord(request,accountDO.getUserId(),accountDO.getLoginName(),profileDO.getName(),profileDO.getSpaceId());
        }
        return accountDO;
    }

    @Override
    public boolean isSupport(Integer site) {
        return true;
    }

}
